********************************************************************************
**  TITLE:      on2014_ipsos_exit			                                  **  
**  AUTHORS:    P. Mongrain, N. Fréchet, B. Thompson Collart, and Y. Dufresne **
**  DATA:       2014 Ontario General Election Exit Survey                     **
**	DATE:		February 2025	 					                          **	
**  VERSION:	Stata 17					                                  **	
********************************************************************************

* Note: The data file needs to be downloaded from its original source (see the "surveys.text" file)

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "on2014_ipsos_exit", replace

* Open the dataset

import spss using "2014 Ontario General Election Exit Survey.sav", clear case(lower)

* Election

gen election = "on2014"

* Type of survey

gen type = "exit"

* Respondent ID

gen responseid = _n

* Survey weight

merge 1:1 election responseid using on2014_weight
drop _merge


***********************
** DISTRICT OUTCOMES **
***********************

gen district_code = riding

recode district_code 35010=35011 35011=35012 35012=35013 35013=35014 35014=35019 35019=35020 35020=35021 35021=35022 35022=35023 35023=35024 35024=35025 35025=35026 35026=35010 35040=35041 35041=35042 35042=35043 35043=35044 35044=35045 35045=35046 35046=35040 35092=35093 35093=35094 35094=35095 35095=35096 35096=35097 35097=35098 35098=35099 35099=35100 35100=35101 35101=35102 35102=35103 35103=35104 35104=35105 35105=35106 35106=35107 // Correct wrong codes in dataset

gen district_name = ""

replace district_name = "Ajax - Pickering" if district_code == 35001
replace district_name = "Algoma - Manitoulin" if district_code == 35002
replace district_name = "Ancaster - Dundas - Flamborough - Westdale" if district_code == 35003
replace district_name = "Barrie, Barrie" if district_code == 35004
replace district_name = "Beaches - East York" if district_code == 35005
replace district_name = "Bramalea - Gore - Malton" if district_code == 35006
replace district_name = "Brampton - Springdale, Brampton - Springdale" if district_code == 35007
replace district_name = "Brampton West" if district_code == 35008
replace district_name = "Brant" if district_code == 35009
replace district_name = "Bruce - Grey - Owen Sound" if district_code == 35010
replace district_name = "Burlington" if district_code == 35011
replace district_name = "Cambridge" if district_code == 35012
replace district_name = "Carleton - Mississippi Mills" if district_code == 35013
replace district_name = "Chatham - Kent - Essex" if district_code == 35014
replace district_name = "Davenport" if district_code == 35015
replace district_name = "Don Valley East" if district_code == 35016
replace district_name = "Don Valley West" if district_code == 35017
replace district_name = "Dufferin - Caledon" if district_code == 35018
replace district_name = "Durham" if district_code == 35019
replace district_name = "Eglinton - Lawrence" if district_code == 35020
replace district_name = "Elgin - Middlesex - London" if district_code == 35021
replace district_name = "Essex" if district_code == 35022
replace district_name = "Etobicoke Centre" if district_code == 35023
replace district_name = "Etobicoke - Lakeshore" if district_code == 35024
replace district_name = "Etobicoke North" if district_code == 35025
replace district_name = "Glengarry - Prescott - Russell" if district_code == 35026
replace district_name = "Guelph" if district_code == 35027
replace district_name = "Haldimand - Norfolk" if district_code == 35028
replace district_name = "Haliburton - Kawartha Lakes - Brock" if district_code == 35029
replace district_name = "Halton" if district_code == 35030
replace district_name = "Hamilton Centre" if district_code == 35031
replace district_name = "Hamilton East - Stoney Creek" if district_code == 35032
replace district_name = "Hamilton Mountain" if district_code == 35033
replace district_name = "Huron - Bruce" if district_code == 35034
replace district_name = "Kenora - Rainy River" if district_code == 35035
replace district_name = "Kingston And The Islands" if district_code == 35036
replace district_name = "Kitchener Centre" if district_code == 35037
replace district_name = "Kitchener - Conestoga" if district_code == 35038
replace district_name = "Kitchener - Waterloo" if district_code == 35039
replace district_name = "Lambton - Kent - Middlesex" if district_code == 35040
replace district_name = "Lanark - Frontenac - Lennox And Addington" if district_code == 35041
replace district_name = "Leeds - Grenville" if district_code == 35042
replace district_name = "London - Fanshawe" if district_code == 35043
replace district_name = "London North Centre" if district_code == 35044
replace district_name = "London West" if district_code == 35045
replace district_name = "Markham - Unionville" if district_code == 35046
replace district_name = "Mississauga - Brampton South" if district_code == 35047
replace district_name = "Mississauga East - Cooksville" if district_code == 35048
replace district_name = "Mississauga - Erindale" if district_code == 35049
replace district_name = "Mississauga South" if district_code == 35050
replace district_name = "Mississauga - Streetsville" if district_code == 35051
replace district_name = "Nepean - Carleton" if district_code == 35052
replace district_name = "Newmarket - Aurora" if district_code == 35053
replace district_name = "Niagara Falls" if district_code == 35054
replace district_name = "Niagara West - Glanbrook" if district_code == 35055
replace district_name = "Nickel Belt" if district_code == 35056
replace district_name = "Nipissing" if district_code == 35057
replace district_name = "Northumberland - Quinte West" if district_code == 35058
replace district_name = "Oak Ridges - Markham" if district_code == 35059
replace district_name = "Oakville" if district_code == 35060
replace district_name = "Oshawa" if district_code == 35061
replace district_name = "Ottawa Centre" if district_code == 35062
replace district_name = "Ottawa - Orleans" if district_code == 35063
replace district_name = "Ottawa South" if district_code == 35064
replace district_name = "Ottawa - Vanier" if district_code == 35065
replace district_name = "Ottawa West - Nepean" if district_code == 35066
replace district_name = "Oxford" if district_code == 35067
replace district_name = "Parkdale - High Park" if district_code == 35068
replace district_name = "Parry Sound - Muskoka" if district_code == 35069
replace district_name = "Perth - Wellington" if district_code == 35070
replace district_name = "Peterborough" if district_code == 35071
replace district_name = "Pickering - Scarborough East" if district_code == 35072
replace district_name = "Prince Edward - Hastings" if district_code == 35073
replace district_name = "Renfrew - Nipissing - Pembroke" if district_code == 35074
replace district_name = "Richmond Hill" if district_code == 35075
replace district_name = "St. Catharines" if district_code == 35076
replace district_name = "St. Paul's" if district_code == 35077
replace district_name = "Sarnia - Lambton" if district_code == 35078
replace district_name = "Sault Ste. Marie" if district_code == 35079
replace district_name = "Scarborough - Agincourt" if district_code == 35080
replace district_name = "Scarborough Centre" if district_code == 35081
replace district_name = "Scarborough - Guildwood" if district_code == 35082
replace district_name = "Scarborough - Rouge River" if district_code == 35083
replace district_name = "Scarborough Southwest" if district_code == 35084
replace district_name = "Simcoe - Grey" if district_code == 35085
replace district_name = "Simcoe North" if district_code == 35086
replace district_name = "Stormont - Dundas - South Glengarry" if district_code == 35087
replace district_name = "Sudbury" if district_code == 35088
replace district_name = "Thornhill" if district_code == 35089
replace district_name = "Thunder Bay - Atikokan" if district_code == 35090
replace district_name = "Thunder Bay - Superior North" if district_code == 35091
replace district_name = "Timmins - James Bay" if district_code == 35093
replace district_name = "Toronto Centre" if district_code == 35094
replace district_name = "Toronto - Danforth" if district_code == 35095
replace district_name = "Trinity - Spadina" if district_code == 35096
replace district_name = "Vaughan" if district_code == 35097
replace district_name = "Welland" if district_code == 35098
replace district_name = "Wellington - Halton Hills" if district_code == 35099
replace district_name = "Whitby - Oshawa" if district_code == 35100
replace district_name = "Willowdale" if district_code == 35101
replace district_name = "Windsor - Tecumseh" if district_code == 35102
replace district_name = "Windsor West" if district_code == 35103
replace district_name = "York Centre" if district_code == 35104
replace district_name = "York - Simcoe" if district_code == 35105
replace district_name = "York South - Weston" if district_code == 35106
replace district_name = "York West" if district_code == 35107

* Election outcome in each district 

gen district_outcome = .

replace district_outcome = 352 if district_code == 35001
replace district_outcome = 353 if district_code == 35002
replace district_outcome = 352 if district_code == 35003
replace district_outcome = 352 if district_code == 35004
replace district_outcome = 352 if district_code == 35005
replace district_outcome = 353 if district_code == 35006
replace district_outcome = 352 if district_code == 35007
replace district_outcome = 352 if district_code == 35008
replace district_outcome = 352 if district_code == 35009
replace district_outcome = 351 if district_code == 35010
replace district_outcome = 352 if district_code == 35011
replace district_outcome = 352 if district_code == 35012
replace district_outcome = 351 if district_code == 35013
replace district_outcome = 351 if district_code == 35014
replace district_outcome = 352 if district_code == 35015
replace district_outcome = 352 if district_code == 35016
replace district_outcome = 352 if district_code == 35017
replace district_outcome = 351 if district_code == 35018
replace district_outcome = 352 if district_code == 35019
replace district_outcome = 352 if district_code == 35020
replace district_outcome = 351 if district_code == 35021
replace district_outcome = 353 if district_code == 35022
replace district_outcome = 352 if district_code == 35023
replace district_outcome = 352 if district_code == 35024
replace district_outcome = 352 if district_code == 35025
replace district_outcome = 352 if district_code == 35026
replace district_outcome = 352 if district_code == 35027
replace district_outcome = 351 if district_code == 35028
replace district_outcome = 351 if district_code == 35029
replace district_outcome = 352 if district_code == 35030
replace district_outcome = 353 if district_code == 35031
replace district_outcome = 353 if district_code == 35032
replace district_outcome = 353 if district_code == 35033
replace district_outcome = 351 if district_code == 35034
replace district_outcome = 353 if district_code == 35035
replace district_outcome = 352 if district_code == 35036
replace district_outcome = 352 if district_code == 35037
replace district_outcome = 351 if district_code == 35038
replace district_outcome = 353 if district_code == 35039
replace district_outcome = 351 if district_code == 35040
replace district_outcome = 351 if district_code == 35041
replace district_outcome = 351 if district_code == 35042
replace district_outcome = 353 if district_code == 35043
replace district_outcome = 352 if district_code == 35044
replace district_outcome = 353 if district_code == 35045
replace district_outcome = 352 if district_code == 35046
replace district_outcome = 352 if district_code == 35047
replace district_outcome = 352 if district_code == 35048
replace district_outcome = 352 if district_code == 35049
replace district_outcome = 352 if district_code == 35050
replace district_outcome = 352 if district_code == 35051
replace district_outcome = 351 if district_code == 35052
replace district_outcome = 352 if district_code == 35053
replace district_outcome = 353 if district_code == 35054
replace district_outcome = 351 if district_code == 35055
replace district_outcome = 353 if district_code == 35056
replace district_outcome = 351 if district_code == 35057
replace district_outcome = 352 if district_code == 35058
replace district_outcome = 352 if district_code == 35059
replace district_outcome = 352 if district_code == 35060
replace district_outcome = 353 if district_code == 35061
replace district_outcome = 352 if district_code == 35062
replace district_outcome = 352 if district_code == 35063
replace district_outcome = 352 if district_code == 35064
replace district_outcome = 352 if district_code == 35065
replace district_outcome = 352 if district_code == 35066
replace district_outcome = 351 if district_code == 35067
replace district_outcome = 353 if district_code == 35068
replace district_outcome = 351 if district_code == 35069
replace district_outcome = 351 if district_code == 35070
replace district_outcome = 352 if district_code == 35071
replace district_outcome = 352 if district_code == 35072
replace district_outcome = 351 if district_code == 35073
replace district_outcome = 351 if district_code == 35074
replace district_outcome = 352 if district_code == 35075
replace district_outcome = 352 if district_code == 35076
replace district_outcome = 352 if district_code == 35077
replace district_outcome = 351 if district_code == 35078
replace district_outcome = 352 if district_code == 35079
replace district_outcome = 352 if district_code == 35080
replace district_outcome = 352 if district_code == 35081
replace district_outcome = 352 if district_code == 35082
replace district_outcome = 352 if district_code == 35083
replace district_outcome = 352 if district_code == 35084
replace district_outcome = 351 if district_code == 35085
replace district_outcome = 351 if district_code == 35086
replace district_outcome = 351 if district_code == 35087
replace district_outcome = 353 if district_code == 35088
replace district_outcome = 351 if district_code == 35089
replace district_outcome = 352 if district_code == 35090
replace district_outcome = 352 if district_code == 35091
replace district_outcome = 353 if district_code == 35092
replace district_outcome = 353 if district_code == 35093
replace district_outcome = 352 if district_code == 35094
replace district_outcome = 353 if district_code == 35095
replace district_outcome = 352 if district_code == 35096
replace district_outcome = 352 if district_code == 35097
replace district_outcome = 353 if district_code == 35098
replace district_outcome = 351 if district_code == 35099
replace district_outcome = 351 if district_code == 35100
replace district_outcome = 352 if district_code == 35101
replace district_outcome = 353 if district_code == 35102
replace district_outcome = 353 if district_code == 35103
replace district_outcome = 352 if district_code == 35104
replace district_outcome = 351 if district_code == 35105
replace district_outcome = 352 if district_code == 35106
replace district_outcome = 352 if district_code == 35107

label define outcome 351 "Progressive Conservative Party of Ontario" 352 "Ontario Liberal Party" 353 "Ontario New Democratic Party" 88 "Other"
label values district_outcome outcome


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by district

gen margin = .

replace margin = 21.89421 if district_code == 35001
replace margin = 28.89609 if district_code == 35002
replace margin = 10.73037 if district_code == 35003
replace margin = 4.595234 if district_code == 35004
replace margin = 1.119957 if district_code == 35005
replace margin = 10.63838 if district_code == 35006
replace margin = 8.080471 if district_code == 35007
replace margin = 20.88995 if district_code == 35008
replace margin = 7.661415 if district_code == 35009
replace margin = 20.49 if district_code == 35010
replace margin = 6.42067 if district_code == 35011
replace margin = 6.367088 if district_code == 35012
replace margin = 15.7063 if district_code == 35013
replace margin = 6.719126 if district_code == 35014
replace margin = 5.466167 if district_code == 35015
replace margin = 28.91919 if district_code == 35016
replace margin = 26.38756 if district_code == 35017
replace margin = 9.194082 if district_code == 35018
replace margin = 2.163113 if district_code == 35019
replace margin = 21.04253 if district_code == 35020
replace margin = 19.72423 if district_code == 35021
replace margin = 38.51717 if district_code == 35022
replace margin = 17.55777 if district_code == 35023
replace margin = 13.13461 if district_code == 35024
replace margin = 18.69142 if district_code == 35025
replace margin = 17.17361 if district_code == 35026
replace margin = 20.68081 if district_code == 35027
replace margin = 29.06234 if district_code == 35028
replace margin = 5.921761 if district_code == 35029
replace margin = 7.68536 if district_code == 35030
replace margin = 28.50348 if district_code == 35031
replace margin = 17.64899 if district_code == 35032
replace margin = 17.32341 if district_code == 35033
replace margin = 8.144213 if district_code == 35034
replace margin = 30.15935 if district_code == 35035
replace margin = 12.02778 if district_code == 35036
replace margin = 16.16685 if district_code == 35037
replace margin = 3.020241 if district_code == 35038
replace margin = 7.294933 if district_code == 35039
replace margin = 18.64898 if district_code == 35040
replace margin = 13.72895 if district_code == 35041
replace margin = 35.57581 if district_code == 35042
replace margin = 27.3375 if district_code == 35043
replace margin = 5.549578 if district_code == 35044
replace margin = 10.79375 if district_code == 35045
replace margin = 17.35853 if district_code == 35046
replace margin = 20.98589 if district_code == 35047
replace margin = 26.13684 if district_code == 35048
replace margin = 19.09049 if district_code == 35049
replace margin = 17.56216 if district_code == 35050
replace margin = 24.50191 if district_code == 35051
replace margin = 13.51081 if district_code == 35052
replace margin = 6.519787 if district_code == 35053
replace margin = 14.58869 if district_code == 35054
replace margin = 13.47895 if district_code == 35055
replace margin = 40.71698 if district_code == 35056
replace margin = 15.027 if district_code == 35057
replace margin = 7.039051 if district_code == 35058
replace margin = 8.08123 if district_code == 35059
replace margin = 11.58343 if district_code == 35060
replace margin = 16.15695 if district_code == 35061
replace margin = 31.55057 if district_code == 35062
replace margin = 20.36669 if district_code == 35063
replace margin = 17.85669 if district_code == 35064
replace margin = 33.26456 if district_code == 35065
replace margin = 10.95785 if district_code == 35066
replace margin = 20.45073 if district_code == 35067
replace margin = 1.206394 if district_code == 35068
replace margin = 14.47878 if district_code == 35069
replace margin = 5.863865 if district_code == 35070
replace margin = 16.50447 if district_code == 35071
replace margin = 23.66217 if district_code == 35072
replace margin = 9.035439 if district_code == 35073
replace margin = 41.96061 if district_code == 35074
replace margin = 11.24322 if district_code == 35075
replace margin = 11.29982 if district_code == 35076
replace margin = 35.79316 if district_code == 35077
replace margin = 5.246437 if district_code == 35078
replace margin = 33.06117 if district_code == 35079
replace margin = 15.21627 if district_code == 35080
replace margin = 33.47814 if district_code == 35081
replace margin = 21.88643 if district_code == 35082
replace margin = 7.397257 if district_code == 35083
replace margin = 26.57541 if district_code == 35084
replace margin = 15.93596 if district_code == 35085
replace margin = 11.42891 if district_code == 35086
replace margin = 28.52128 if district_code == 35087
replace margin = 2.894093 if district_code == 35088
replace margin = .2130623 if district_code == 35089
replace margin = 24.86823 if district_code == 35090
replace margin = 26.51037 if district_code == 35091
replace margin = 32.26746 if district_code == 35092
replace margin = 27.11562 if district_code == 35093
replace margin = 39.91991 if district_code == 35094
replace margin = 7.455196 if district_code == 35095
replace margin = 15.96789 if district_code == 35096
replace margin = 28.03763 if district_code == 35097
replace margin = 18.38433 if district_code == 35098
replace margin = 17.29472 if district_code == 35099
replace margin = 9.152428 if district_code == 35100
replace margin = 19.10902 if district_code == 35101
replace margin = 46.90548 if district_code == 35102
replace margin = 2.868073 if district_code == 35103
replace margin = 16.43053 if district_code == 35104
replace margin = 5.838253 if district_code == 35105
replace margin = 10.59301 if district_code == 35106
replace margin = 7.491959 if district_code == 35107

* Effective number of electoral parties by district

gen enep = .

replace enep = 2.682988 if district_code == 35001
replace enep = 2.65602 if district_code == 35002
replace enep = 2.943528 if district_code == 35003
replace enep = 3.064585 if district_code == 35004
replace enep = 2.98422 if district_code == 35005
replace enep = 2.915204 if district_code == 35006
replace enep = 3.11343 if district_code == 35007
replace enep = 3.114605 if district_code == 35008
replace enep = 3.259235 if district_code == 35009
replace enep = 3.012555 if district_code == 35010
replace enep = 2.877239 if district_code == 35011
replace enep = 3.251334 if district_code == 35012
replace enep = 2.856908 if district_code == 35013
replace enep = 3.305314 if district_code == 35014
replace enep = 2.649685 if district_code == 35015
replace enep = 2.49654 if district_code == 35016
replace enep = 2.348813 if district_code == 35017
replace enep = 3.39809 if district_code == 35018
replace enep = 3.221646 if district_code == 35019
replace enep = 2.377106 if district_code == 35020
replace enep = 3.031213 if district_code == 35021
replace enep = 2.308205 if district_code == 35022
replace enep = 2.663435 if district_code == 35023
replace enep = 2.772435 if district_code == 35024
replace enep = 3.091391 if district_code == 35025
replace enep = 2.701313 if district_code == 35026
replace enep = 3.51691 if district_code == 35027
replace enep = 2.720118 if district_code == 35028
replace enep = 3.018603 if district_code == 35029
replace enep = 2.805768 if district_code == 35030
replace enep = 2.826093 if district_code == 35031
replace enep = 2.961627 if district_code == 35032
replace enep = 2.929102 if district_code == 35033
replace enep = 3.314562 if district_code == 35034
replace enep = 2.496052 if district_code == 35035
replace enep = 3.219828 if district_code == 35036
replace enep = 3.180483 if district_code == 35037
replace enep = 3.406 if district_code == 35038
replace enep = 3.298907 if district_code == 35039
replace enep = 3.144817 if district_code == 35040
replace enep = 3.094636 if district_code == 35041
replace enep = 2.569388 if district_code == 35042
replace enep = 2.866155 if district_code == 35043
replace enep = 3.391019 if district_code == 35044
replace enep = 3.237906 if district_code == 35045
replace enep = 2.561364 if district_code == 35046
replace enep = 2.973007 if district_code == 35047
replace enep = 2.718354 if district_code == 35048
replace enep = 2.83546 if district_code == 35049
replace enep = 2.628225 if district_code == 35050
replace enep = 2.663286 if district_code == 35051
replace enep = 2.860406 if district_code == 35052
replace enep = 2.869297 if district_code == 35053
replace enep = 2.823279 if district_code == 35054
replace enep = 3.248786 if district_code == 35055
replace enep = 2.195361 if district_code == 35056
replace enep = 3.180335 if district_code == 35057
replace enep = 2.90589 if district_code == 35058
replace enep = 2.756825 if district_code == 35059
replace enep = 2.532538 if district_code == 35060
replace enep = 2.866184 if district_code == 35061
replace enep = 2.842999 if district_code == 35062
replace enep = 2.466012 if district_code == 35063
replace enep = 2.705149 if district_code == 35064
replace enep = 2.614521 if district_code == 35065
replace enep = 2.936331 if district_code == 35066
replace enep = 3.046914 if district_code == 35067
replace enep = 2.921165 if district_code == 35068
replace enep = 3.461053 if district_code == 35069
replace enep = 3.333344 if district_code == 35070
replace enep = 2.951566 if district_code == 35071
replace enep = 2.67926 if district_code == 35072
replace enep = 3.122183 if district_code == 35073
replace enep = 2.316963 if district_code == 35074
replace enep = 2.666526 if district_code == 35075
replace enep = 3.151051 if district_code == 35076
replace enep = 2.341503 if district_code == 35077
replace enep = 3.028505 if district_code == 35078
replace enep = 2.359602 if district_code == 35079
replace enep = 2.610261 if district_code == 35080
replace enep = 2.55264 if district_code == 35081
replace enep = 2.798846 if district_code == 35082
replace enep = 3.080731 if district_code == 35083
replace enep = 2.835829 if district_code == 35084
replace enep = 2.89878 if district_code == 35085
replace enep = 3.033885 if district_code == 35086
replace enep = 2.732996 if district_code == 35087
replace enep = 2.827958 if district_code == 35088
replace enep = 2.547315 if district_code == 35089
replace enep = 2.641537 if district_code == 35090
replace enep = 2.452728 if district_code == 35091
replace enep = 2.551624 if district_code == 35092
replace enep = 2.689279 if district_code == 35093
replace enep = 2.476943 if district_code == 35094
replace enep = 2.855143 if district_code == 35095
replace enep = 3.015212 if district_code == 35096
replace enep = 2.442397 if district_code == 35097
replace enep = 2.94433 if district_code == 35098
replace enep = 3.038859 if district_code == 35099
replace enep = 3.131125 if district_code == 35100
replace enep = 2.498873 if district_code == 35101
replace enep = 2.295258 if district_code == 35102
replace enep = 2.92161 if district_code == 35103
replace enep = 2.81608 if district_code == 35104
replace enep = 3.137647 if district_code == 35105
replace enep = 2.624199 if district_code == 35106
replace enep = 2.602051 if district_code == 35107

* Competition measure

gen competition = .

replace competition = .3509298 if district_code == 35001
replace competition = .3861388 if district_code == 35002
replace competition = .319926 if district_code == 35003
replace competition = .293558 if district_code == 35004
replace competition = .2558888 if district_code == 35005
replace competition = .3110908 if district_code == 35006
replace competition = .3151435 if district_code == 35007
replace competition = .3973422 if district_code == 35008
replace competition = .3286347 if district_code == 35009
replace competition = .3942699 if district_code == 35010
replace competition = .2776826 if district_code == 35011
replace competition = .3284662 if district_code == 35012
replace competition = .3422568 if district_code == 35013
replace competition = .3341811 if district_code == 35014
replace competition = .2310096 if district_code == 35015
replace competition = .3653385 if district_code == 35016
replace competition = .3256067 if district_code == 35017
replace competition = .3723297 if district_code == 35018
replace competition = .290998 if district_code == 35019
replace competition = .2943253 if district_code == 35020
replace competition = .3856011 if district_code == 35021
replace competition = .3767031 if district_code == 35022
replace competition = .3242636 if district_code == 35023
replace competition = .31181 if district_code == 35024
replace competition = .383262 if district_code == 35025
replace competition = .3281684 if district_code == 35026
replace competition = .4428956 if district_code == 35027
replace competition = .391029 if district_code == 35028
replace competition = .2912193 if district_code == 35029
replace competition = .2759596 if district_code == 35030
replace competition = .4150943 if district_code == 35031
replace competition = .3674935 if district_code == 35032
replace competition = .3597167 if district_code == 35033
replace competition = .3482511 if district_code == 35034
replace competition = .3670492 if district_code == 35035
replace competition = .361769 if district_code == 35036
replace competition = .3797625 if district_code == 35037
replace competition = .3282558 if district_code == 35038
replace competition = .3334706 if district_code == 35039
replace competition = .3962704 if district_code == 35040
replace competition = .3565604 if district_code == 35041
replace competition = .40426 if district_code == 35042
replace competition = .4057532 if district_code == 35043
replace competition = .335546 if district_code == 35044
replace competition = .3526709 if district_code == 35045
replace competition = .3045846 if district_code == 35046
replace competition = .392957 if district_code == 35047
replace competition = .3852143 if district_code == 35048
replace competition = .3619223 if district_code == 35049
replace competition = .3191405 if district_code == 35050
replace competition = .3685824 if district_code == 35051
replace competition = .3289944 if district_code == 35052
replace competition = .2802106 if district_code == 35053
replace competition = .3291435 if district_code == 35054
replace competition = .374276 if district_code == 35055
replace competition = .3676662 if district_code == 35056
replace competition = .3664892 if district_code == 35057
replace competition = .284072 if district_code == 35058
replace competition = .2711098 if district_code == 35059
replace competition = .2555338 if district_code == 35060
replace competition = .3391014 if district_code == 35061
replace competition = .4254682 if district_code == 35062
replace competition = .3075542 if district_code == 35063
replace competition = .3336516 if district_code == 35064
replace competition = .4077142 if district_code == 35065
replace competition = .3214207 if district_code == 35066
replace competition = .3901498 if district_code == 35067
replace competition = .2460476 if district_code == 35068
replace competition = .4085286 if district_code == 35069
replace competition = .3415742 if district_code == 35070
replace competition = .3573019 if district_code == 35071
replace competition = .3642005 if district_code == 35072
replace competition = .3314128 if district_code == 35073
replace competition = .39372 if district_code == 35074
replace competition = .2786703 if district_code == 35075
replace competition = .3415957 if district_code == 35076
replace competition = .3771596 if district_code == 35077
replace competition = .2907411 if district_code == 35078
replace competition = .3628559 if district_code == 35079
replace competition = .2967495 if district_code == 35080
replace competition = .3834967 if district_code == 35081
replace competition = .3698232 if district_code == 35082
replace competition = .297623 if district_code == 35083
replace competition = .3945908 if district_code == 35084
replace competition = .3499017 if district_code == 35085
replace competition = .3383002 if district_code == 35086
replace competition = .3878891 if district_code == 35087
replace competition = .2412677 if district_code == 35088
replace competition = .1630548 if district_code == 35089
replace competition = .3678146 if district_code == 35090
replace competition = .3488803 if district_code == 35091
replace competition = .3862628 if district_code == 35092
replace competition = .3671512 if district_code == 35093
replace competition = .4115545 if district_code == 35094
replace competition = .2859191 if district_code == 35095
replace competition = .3698382 if district_code == 35096
replace competition = .3518236 if district_code == 35097
replace competition = .3650438 if district_code == 35098
replace competition = .38178 if district_code == 35099
replace competition = .3267974 if district_code == 35100
replace competition = .3040113 if district_code == 35101
replace competition = .4042947 if district_code == 35102
replace competition = .2582026 if district_code == 35103
replace competition = .338254 if district_code == 35104
replace competition = .3125281 if district_code == 35105
replace competition = .264616 if district_code == 35106
replace competition = .2352513 if district_code == 35107


*******************************
** INCUMBENT PARTY REELECTED **
*******************************

gen reelected = .

replace reelected = 1 if district_code == 35001
replace reelected = 1 if district_code == 35002
replace reelected = 1 if district_code == 35003
replace reelected = 0 if district_code == 35004
replace reelected = 0 if district_code == 35005
replace reelected = 1 if district_code == 35006
replace reelected = 1 if district_code == 35007
replace reelected = 1 if district_code == 35008
replace reelected = 1 if district_code == 35009
replace reelected = 1 if district_code == 35010
replace reelected = 0 if district_code == 35011
replace reelected = 0 if district_code == 35012
replace reelected = 1 if district_code == 35013
replace reelected = 1 if district_code == 35014
replace reelected = 0 if district_code == 35015
replace reelected = 1 if district_code == 35016
replace reelected = 1 if district_code == 35017
replace reelected = 1 if district_code == 35018
replace reelected = 0 if district_code == 35019
replace reelected = 1 if district_code == 35020
replace reelected = 1 if district_code == 35021
replace reelected = 1 if district_code == 35022
replace reelected = 1 if district_code == 35023
replace reelected = 1 if district_code == 35024
replace reelected = 1 if district_code == 35025
replace reelected = 1 if district_code == 35026
replace reelected = 1 if district_code == 35027
replace reelected = 1 if district_code == 35028
replace reelected = 1 if district_code == 35029
replace reelected = 0 if district_code == 35030
replace reelected = 1 if district_code == 35031
replace reelected = 1 if district_code == 35032
replace reelected = 1 if district_code == 35033
replace reelected = 1 if district_code == 35034
replace reelected = 1 if district_code == 35035
replace reelected = 1 if district_code == 35036
replace reelected = 1 if district_code == 35037
replace reelected = 1 if district_code == 35038
replace reelected = 0 if district_code == 35039
replace reelected = 1 if district_code == 35040
replace reelected = 1 if district_code == 35041
replace reelected = 1 if district_code == 35042
replace reelected = 1 if district_code == 35043
replace reelected = 1 if district_code == 35044
replace reelected = 0 if district_code == 35045
replace reelected = 1 if district_code == 35046
replace reelected = 1 if district_code == 35047
replace reelected = 1 if district_code == 35048
replace reelected = 1 if district_code == 35049
replace reelected = 1 if district_code == 35050
replace reelected = 1 if district_code == 35051
replace reelected = 1 if district_code == 35052
replace reelected = 0 if district_code == 35053
replace reelected = 0 if district_code == 35054
replace reelected = 1 if district_code == 35055
replace reelected = 1 if district_code == 35056
replace reelected = 1 if district_code == 35057
replace reelected = 0 if district_code == 35058
replace reelected = 1 if district_code == 35059
replace reelected = 1 if district_code == 35060
replace reelected = 0 if district_code == 35061
replace reelected = 1 if district_code == 35062
replace reelected = 1 if district_code == 35063
replace reelected = 1 if district_code == 35064
replace reelected = 1 if district_code == 35065
replace reelected = 1 if district_code == 35066
replace reelected = 1 if district_code == 35067
replace reelected = 1 if district_code == 35068
replace reelected = 1 if district_code == 35069
replace reelected = 1 if district_code == 35070
replace reelected = 1 if district_code == 35071
replace reelected = 1 if district_code == 35072
replace reelected = 1 if district_code == 35073
replace reelected = 1 if district_code == 35074
replace reelected = 1 if district_code == 35075
replace reelected = 1 if district_code == 35076
replace reelected = 1 if district_code == 35077
replace reelected = 1 if district_code == 35078
replace reelected = 1 if district_code == 35079
replace reelected = 1 if district_code == 35080
replace reelected = 1 if district_code == 35081
replace reelected = 1 if district_code == 35082
replace reelected = 1 if district_code == 35083
replace reelected = 1 if district_code == 35084
replace reelected = 1 if district_code == 35085
replace reelected = 1 if district_code == 35086
replace reelected = 1 if district_code == 35087
replace reelected = 0 if district_code == 35088
replace reelected = 1 if district_code == 35089
replace reelected = 1 if district_code == 35090
replace reelected = 1 if district_code == 35091
replace reelected = 1 if district_code == 35092
replace reelected = 1 if district_code == 35093
replace reelected = 1 if district_code == 35094
replace reelected = 1 if district_code == 35095
replace reelected = 0 if district_code == 35096
replace reelected = 1 if district_code == 35097
replace reelected = 1 if district_code == 35098
replace reelected = 1 if district_code == 35099
replace reelected = 1 if district_code == 35100
replace reelected = 1 if district_code == 35101
replace reelected = 0 if district_code == 35102
replace reelected = 0 if district_code == 35103
replace reelected = 1 if district_code == 35104
replace reelected = 1 if district_code == 35105
replace reelected = 1 if district_code == 35106
replace reelected = 1 if district_code == 35107

label define reelected 0 "Not reelected" 1 "Reelected"
label values reelected reelected


***************************
** INDEPENDENT VARIABLES **
***************************

* Province

gen province = 5

label define province 5 "Ontario"
label values province province

* Region

gen regionON = mrk_region

* Gender

gen male = resp_gender

recode male (2=0)

label define gender 0 "Female" 1 "Male"
label values male gender

* Age

gen age = age_num

* Age 55 and over

gen age55 = 1 if age >= 55
replace age55 = 0 if age < 55 & age!=.

label define age55 0 "Less than 55" 1 "55 and over"
label values age55 age55

* Education

gen education = quota_education

recode education 1=991 2=992 3=993 4=994

label define education 991 "Less than high school" 992 "High school" 993 "Some postsecondary education" 994 "University graduate"
label values education education

* University degree

gen univ = 1 if education == 994
replace univ = 0 if education < 994 & education!=.

label define univ 0 "No university degree" 1 "University degree"
label values univ univ

* Postsecondary degree

gen postsec = 1 if education >= 993
replace postsec = 0 if education < 993 & education!=.

label define postsec 0 "No postsecondary" 1 "Postsecondary"
label values postsec postsec

* Immigrant

gen immigrant = q40

recode immigrant 1=0 2=1

label define immigrant 0 "Born in Canada" 1 "Immigrant"
label values immigrant immigrant

* Visible minority

gen minority = q54

recode minority 2=0

label define minority 0 "Not visible minority" 1 "Visible minority"
label values minority minority

* Household income

gen income = ushhi2

recode income 1=1001 2=1002 3=1003 4=1004 5=1005 6=1006 7=1007 8=1008 9=1009 10=1010 11=1011 12=1012 13=1013 14=1014 15=1015 16=1016 17=1017 18=1018 19=1019 20=1020 21=1021 22=1022 23=1023 24=.

label define income 1001 "Less than $5,000" 1002 "$5,000-$9,999" 1003 "$10,000-$14,999" 1004 "$15,000-$19,999" 1005 "$20,000-$24,999" 1006 "$25,000-$29,999" 1007 "$30,000-$34,999" 1008 "$35,000-$39,999" 1009 "$40,000-$44,999" 1010 "$45,000-$49,999" 1011 "$50,000-$54,999" 1012 "$55,000-$59,999" 1013 "$60,000-$64,999" 1014 "$65,000-$69,999" 1015 "$70,000-$74,999" 1016 "$75,000-$79,999" 1017 "$80,000-$89,999" 1018 "$90,000-$99,999" 1019 "$100,000-$124,999" 1020 "$125,000-$149,999" 1021 "$150,000-199,999$" 1022 "$200,000-249,999$" 1023 "$250,000 or more"
label values income income

* Household income (quintile)

xtile income5 = income, nq(5)

* Highest income categories

gen highinc = 1 if income5 == 5
replace highinc = 0 if income5 < 5 & income!=.

label define highinc 0 "Others" 1 "Highest incomes"
label values highinc highinc

* Vote choice

gen vote_choice = q3

recode vote_choice 1=351 2=352 3=353 4=88 5=999 6=-99

label define vote_choice 351 "Progressive Conservative Party of Ontario" 352 "Ontario Liberal Party" 353 "Ontario New Democratic Party" 354 "Green Party of Ontario" 88 "Other" 999 "Spoiled ballot" -99 "Don't know" 
label values vote_choice vote_choice

* Vote status (district)

gen vote_district = 1 if vote_choice == district_outcome
replace vote_district = 0 if vote_choice!=district_outcome & vote_choice!=. 

label define winner 0 "Voted for loser" 1 "Voted for winner"
label values vote_district winner

* Vote status (provincial)

gen vote_whole = 1 if vote_choice == 352
replace vote_whole = 0 if vote_choice!=352 & vote_choice!=. 

label values vote_whole winner

* Previous vote

gen first_elxn = q5

gen vote_prev = q6

gen prev = 1 if vote_choice == vote_prev & vote_prev!=.
replace prev = 0 if vote_choice!=vote_prev & vote_prev!=.
replace prev = 888 if vote_prev == 5
replace prev = 999 if first_elxn == 1 | vote_prev == 6

label define prev 0 "Changed vote" 1 "Same vote" 888 "Don't remember" 999 "Did not vote/Not sure"
label values prev prev

* When did you finally decide who you were going to vote for today?

gen decide = q7

recode decide 6=. 1=5 2=4 4=2 5=1


*********************************
** DISTRICT-LEVEL EXPECTATIONS **
*********************************

* Forecasts

gen forecast_district = q13

recode forecast_district 1=351 2=352 3=353 4=88 5=-99

label define forecast 351 "Progressive Conservative Party of Ontario" 352 "Ontario Liberal Party" 353 "Ontario New Democratic Party" 88 "Other" -99 "Don't know"
label values forecast_district forecast

* Identify correct forecasts

gen correct_district = 0 if forecast_district!=district_outcome & forecast_district!=.
replace correct_district = 1 if forecast_district == district_outcome

label define correct 0 "Incorrect" 1 "Correct"
label values correct_district correct


***********************************
** PROVINCIAL-LEVEL EXPECTATIONS **
***********************************

* Forecasts

gen forecast_whole = q14

recode forecast_whole 1=351 2=352 3=353 4=-99

label values forecast_whole forecast

* Identify correct forecasts

gen correct_whole_d = 0 if forecast_whole!=352 & forecast_whole!=.
replace correct_whole_d = 1 if forecast_whole == 352


*************
** WEIGHTS **
*************

* Sex

gen sex_on2014_w = male

recode sex_on2014_w 0=1 1=2

label define sex_on2014_w 1 "Female" 2 "Male"
label values sex_on2014_w sex_on2014_w

* Age

gen age_on2014_w = age_num

replace age_on2014_w = 1 if age_on2014_w >= 18 & age_on2014_w <= 19
replace age_on2014_w = 2 if age_on2014_w >= 20 & age_on2014_w <= 24
replace age_on2014_w = 3 if age_on2014_w >= 25 & age_on2014_w <= 29
replace age_on2014_w = 4 if age_on2014_w >= 30 & age_on2014_w <= 34
replace age_on2014_w = 5 if age_on2014_w >= 35 & age_on2014_w <= 39
replace age_on2014_w = 6 if age_on2014_w >= 40 & age_on2014_w <= 44
replace age_on2014_w = 7 if age_on2014_w >= 45 & age_on2014_w <= 49
replace age_on2014_w = 8 if age_on2014_w >= 50 & age_on2014_w <= 54
replace age_on2014_w = 9 if age_on2014_w >= 55 & age_on2014_w <= 59
replace age_on2014_w = 10 if age_on2014_w >= 60 & age_on2014_w <= 64
replace age_on2014_w = 11 if age_on2014_w >= 65 & age_on2014_w <= 69
replace age_on2014_w = 12 if age_on2014_w >= 70 & age_on2014_w <= 74
replace age_on2014_w = 13 if age_on2014_w >= 75 & age_on2014_w <= 79
replace age_on2014_w = 14 if age_on2014_w >= 80 & age_on2014_w <= 84
replace age_on2014_w = 15 if age_on2014_w >= 85

label define age_on2014_w 1 "18-19" 2 "20-24" 3 "25-29" 4 "30-34" 5 "35-39" 6 "40-44" 7 "45-49" 8 "50-54" 9 "55-59" 10 "60-64" 11 "65-69" 12 "70-74" 13 "75-79" 14 "80-84" 15 "85+"
label values age_on2014_w age_on2014_w

* Education

gen education_on2014_w = quota_education

label define education_on2014_w 1 "Below high school" 2 "High school" 3 "Postsecondary below bachelor" 4 "University degree"
label values education_on2014_w education_on2014_w

* Income

gen income_on2014_w = ushhi2

recode income_on2014_w 20=19 21=20 22=21 23=22 24=.

label define income_on2014_w 1 "Less than $5,000" 2 "$5,000-$9,999" 3 "$10,000-$14,999" 4 "$15,000-$19,999" 5 "$20,000-$24,999" 6 "$25,000-$29,999" 7 "$30,000-$34,999" 8 "$35,000-$39,999" 9 "$40,000-$44,999" 10 "$45,000-$49,999" 11 "$50,000-$54,999" 12 "$55,000-$59,999" 13 "$60,000-$64,999" 14 "$65,000-$69,999" 15 "$70,000-$74,999" 16 "$75,000-$79,999" 17 "$80,000-$89,999" 18 "$90,000-$99,999" 19 "$100,000-$149,999" 20 "$150,000-199,999$" 21 "$200,000-249,999$" 22 "$250,000 or more"
label values income_on2014_w income_on2014_w


**********
** SAVE **
**********

save "on2014_ipsos_exit.dta", replace

log close